/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.test.animation

import com.ages.graphics.AnimatedSprite
import com.ages.graphics.Sprite
import com.ages.input.Keyboard
import javax.media.opengl.GL
import com.ages.entity.Actor
import com.ages.graphics.ViewportSwitcher
import com.ages.graphics.SpriteBufferManager
import com.ages.input.SKeyboard
import com.ages.math.Vector3D
import com.ages.math.Vector
import java.awt.event.KeyEvent._
class Animation extends Actor{

  val sprite = new AnimatedSprite("/com/ages/test/res/fly_anim.png")
  var time = 0
  var frame = 0
  var keys:SKeyboard =_
  var rot = 0
  var offset:Vector = Vector3D(0,0,0)

  def addKeys(k:SKeyboard)
  {
    this.keys = k
  }

  println(sprite)


  def think(){
    if(keys.get(VK_UP))
      pos = pos plus Vector3D(0,1,0)
    if(keys.get(VK_DOWN))
      pos = pos plus Vector3D(0,-1,0)
    if(keys.get(VK_RIGHT))
      pos = pos plus Vector3D(1,0,0)
    if(keys.get(VK_LEFT))
      pos = pos plus Vector3D(-1,0,0)

    if(keys.get(VK_F))
      rot = if(rot>=360) 0 else rot+1
    if(keys.get(VK_G))
      rot = if(rot<= -360) 0 else rot-1

    if(keys.get(VK_W))
      offset = offset plus Vector3D(0,1,0)
    if(keys.get(VK_S))
      offset = offset plus Vector3D(0,-1,0)
    if(keys.get(VK_D))
      offset = offset plus Vector3D(1,0,0)
    if(keys.get(VK_A))
      offset = offset plus Vector3D(-1,0,0)


    println(this)
  }
  def draw(gl:GL){
    time+=1
    if(time>1)
    {
      time=0
      frame=Math.abs(frame-1)
    }
    ViewportSwitcher.setup2DView(gl)
    gl.glPushMatrix
    gl.glTranslated(pos.X,pos.Y,pos.Z)
    gl.glColor4d(0,0,0,1)
    gl.glLineWidth(5f)
    gl.glBegin(GL.GL_LINES)
    gl.glVertex2d(-10,-10)
    gl.glVertex2d(10,10)
    gl.glVertex2d(-10,10)
    gl.glVertex2d(10,-10)
    gl.glEnd
    gl.glRotated(rot,0,0,1)
    gl.glTranslated(offset.X,offset.Y,offset.Z)
    sprite.drawElement(gl,"FLAP_WINGS",frame)
    gl.glPopMatrix
//    println("im drawing")
  }


  override def toString = {
    "Position: "+pos+"\n"+"Offset: "+offset+" \n"+"Rotation: "+rot
  }
}
